/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { CourseVo } from "~/course/api/manage.ts";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";
import { useMessage } from "@/hooks/useMessage.ts";
import { deleteByIds } from "~/course/api/manage";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { ElTag } from "element-plus";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any,
  drawer: any
): MaProTableColumns[] {
  const dictStore = useDictStore();
  const msg = useMessage();

  const showBtn = (auth: string | string[], row: CourseVo) => {
    return hasAuth(auth);
  };

  return [
    // 多选列
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
      selectable: (row: CourseVo) => ![1].includes(row.id as number),
    },
    // 索引序号列
    { type: "index" },
    // 普通列
    {
      label: () => t("course-manage.thumb"),
      prop: "thumb",
      width: "200px",
      cellRender: ({ row }) => (
        <div class="flex-center">
          <el-image src={row.thumb} alt={row.title} />
        </div>
      ),
    },
    { label: () => t("course-manage.title"), prop: "title" },
    { label: () => t("course-category.title"), cellRender({row}){
      if( row.categorys.length == 0 ){
        return <ElTag type="warning">无</ElTag>
      }
      return row.categorys.map(item=><ElTag class="ml-2">{item.name}</ElTag>)
    } ,minWidth:200 },
    { label: () => t("course-tag.title"), cellRender({row}){
      if( row.tags.length == 0 ){
        return <ElTag type="warning">无</ElTag>
      }
      return row.tags.map(item=><ElTag class="ml-2">{item.name}</ElTag>)
    } ,minWidth:200},
    { label: () => t("course-manage.score_option.micro"), prop: "micro_rate" },
    { label: () => t("course-manage.score_option.test"), prop: "test_rate" },
    { label: () => t("course-manage.score_option.live"), prop: "live_rate" },

    {
      label: () => t("course-manage.is_publish"),
      prop: "is_publish",
      cellRender({ row }) {
        return (
          <ElTag type={row.is_publish ? "success" : "warning"}>
            {row.is_publish ? "是" : "否"}
          </ElTag>
        );
      },
    },
    { label: () => t("common.created_at"), prop: "created_at" },
    { label: () => t("common.updated_at"), prop: "updated_at" },
    { label: () => t("common.creator"), prop: "creator.nickname" },
    {
      type: "operation",
      fixed: "right",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "task",
            icon: "material-symbols:time-auto",
            show: ({ row }) => showBtn("course-task:list", row),
            text: () => t("course-task.title"),
            onClick: ({ row }) => {
              drawer.open({ type: "task", data: row });
            },
          },
          {
            name: "file",
            icon: "material-symbols:files",
            show: ({ row }) => showBtn("course-task:file", row),
            text: () => t("course-file.title"),
            onClick: ({ row }) => {
              drawer.open({ type: "file", data: row });
            },
          },
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => showBtn("course-manage:update", row),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            show: ({ row }) => showBtn("course-manage:delete", row),
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
